﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Index2.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
车辆服务管理
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript">
        var id = 'UseVehicleNO';
        var ctrlName = 'VehicleService';
        function Crud2(id, ctrlName) {
            Crud.call(this, id, ctrlName);
            this.Create = function () {
                $('#dlg').dialog('open').dialog('setTitle', '新增');
                $('#fm').form('clear');
                this.url = '../' + ctrlName + '/Create';
                var curDate = new Date().Format('yyyy-MM-dd hh:mm:ss');
                var data = { 'PlanTime': curDate };
                $('#fm').form('load', data);
            };
            this.Edit = function () {
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    this.url = '../' + ctrlName + '/Edit?id=' + row[id];                    
                    $('#dlg').dialog('open').dialog('setTitle', '修改');
                    row['PlanTime'] = JsonToDateString(row['PlanTime'], 'yyyy-MM-dd hh:mm:ss');
                    row['DepartureTime'] = JsonToDateString(row['DepartureTime'], 'yyyy-MM-dd hh:mm:ss');
                    row['ReturnTime'] = JsonToDateString(row['ReturnTime'], 'yyyy-MM-dd hh:mm:ss');                    
                    $('#fm').form('load', row);
                } else {
                    $.messager.alert("提示", "请选择一条记录进行编辑!", 'info');
                }
            };
            this.DoSelect = function (dlgId, url, title, listId, returnId) {
                $.ajaxSetup({ cache: false });
                $(dlgId).dialog({
                    modal: true,
                    width: 450,
                    height: 300,
                    inline: true,
                    title: title,
                    onBeforeOpen: function () {
                        $(listId).load(url);
                    },
                    toolbar: [{
                        id: 'btnOk',
                        text: '确定',
                        iconCls: 'icon-ok',
                        handler: function () {
                            var items = $(':radio:checked');
                            if (items.length > 0) {
                                $(returnId).val(items[0].value);
                            }
                            $(dlgId).dialog('close');
                        }
                    }]
                });
                $(dlgId).dialog('open');
            };
            //派车
            this.IssueVehicle = function () {
                var row = $('#dg').datagrid('getSelected');
                if (!row) {
                    $.messager.alert('提示', '请选择一条记录进行派车!', 'info');
                    return;
                }
                var rowIndex = $('#dg').datagrid('getRowIndex', row);
                var vhid = row[id];
                var startTime = row['DepartureTime'];
                if (startTime) {
                    $.messager.alert('错误', '【' + vhid + '】已经派过车!', 'error');
                    return;
                }
                $.messager.confirm('Confirm', '你确定要给【' + vhid + ' 】派车吗?', function (r) {
                    if (r) {
                        $.post('../' + ctrlName + '/StartVehicle', { id: vhid }, function (result) {
                            if (result.success) {
                                row['DepartureTime'] = result.msg;
                                //更新列表中的派车时间
                                $('#dg').datagrid('updateRow', { index:rowIndex, row:row });
                            } else {
                                $.messager.show({	
                                    title: '错误',
                                    msg: result.msg
                                });
                            }
                        }, 'json');
                    }
                });
            };
            //保存还还信息
            this.Save2 = function () {
                var form = $('#fmReturnVehicle');
                if (!$(form).form('validate')) {
                    return;
                }
                var currRow = this.row;
                $.post(this.url, $(form).serialize(), function (result) {
                    if (result.success) {
                        var rowIndex = $('#dg').datagrid('getRowIndex', currRow);
                        currRow['ReturnTime'] = result.msg;
                        //更新列表中的还车时间
                        $('#dg').datagrid('updateRow', { index: rowIndex, row: currRow });
                        $('#dlgReturnVehicle').dialog('close');
                    } else {
                        $.messager.show({
                            title: '错误',
                            msg: result.msg
                        });
                    }
                });
            };
            this.Cancel2 = function () {
                $('#dlgReturnVehicle').dialog('close');
            };
            //还车
            this.ReturnVehicle = function () {
                var row = $('#dg').datagrid('getSelected');
                if (!row) {
                    $.messager.alert("提示", "请选择一条记录进行还车登记!", 'info');
                    return;
                }
                var vhid = row[id];
                var isReturn = row['ReturnTime'];
                if (isReturn) {
                    $.messager.alert('提示', '【' + vhid + '】已登记还车!', 'info');
                    return;
                }
                var isIssued = row['DepartureTime'];
                if (!isIssued) {
                    $.messager.alert('提示', '【' + vhid + '】还未派出，不能还车!', 'info');
                    return;
                }

                this.url = '../' + ctrlName + '/ReturnVehicle?id=' + row[id];
                this.row = row;
                $('#dlgReturnVehicle').dialog('open').dialog('setTitle', '还车');
                row['PlanTime'] = JsonToDateString(row['PlanTime'], 'yyyy-MM-dd hh:mm:ss');
                row['DepartureTime'] = JsonToDateString(row['DepartureTime'], 'yyyy-MM-dd hh:mm:ss');
                $('#fmReturnVehicle').form('load', row);
            };
            //打印派车单
            this.Print = function () {
                var row = $('#dg').datagrid('getSelected');
                if (!row) {
                    $.messager.alert("提示", "请选择一条记录打印派车单!", 'info');
                    return;
                }
                //打印前检测是否派车
                var isIssued = row['DepartureTime'];
                var vhid = row[id];
                if (!isIssued) {
                    $.messager.alert('提示', '【' + vhid + '】还未派出，请先派车，再打印!', 'info');
                    return;
                }
                var url = '../' + ctrlName + '/PrintBill?id=' + row[id];
                window.open(url, "打印派车单", 'width=800px,location=no,resizable=yes');
            }
        }
        
        var crud = new Crud2(id, ctrlName);
        $(function () {
            //选择司机
            $('#EmployeeNO').click(function () {
                var url = '../' + ctrlName + '/SelectDriver?st=' + $('#PlanTime').val();
                url = encodeURI(url);
                crud.DoSelect('#dlgSelect', url, '选择司机', '#selectList', '#EmployeeNO');
            });

            //选择车辆
            $('#VehicleNO').click(function () {
                var url = '../' + ctrlName + '/SelectVehicle?st=' + $('#PlanTime').val();
                url = encodeURI(url);
                crud.DoSelect('#dlgSelect', url, '选择车辆', '#selectList', '#VehicleNO');
            });

            //扩展工具栏菜单
            toolbar.push('-');
            toolbar.push({
                id: 'btnStartCar',
                text: '派车',
                iconCls: 'icon-back',
                handler: function () {
                    crud.IssueVehicle();
                }
            });
            toolbar.push('-');
            toolbar.push({
                id: 'btnPrint',
                text: '打印派车单',
                iconCls: 'icon-print',
                handler: function () {
                    crud.Print();
                }
            });
            toolbar.push('-');
            toolbar.push({
                id: 'btnEndCar',
                text: '还车',
                iconCls: 'icon-ok',
                handler: function () {
                    crud.ReturnVehicle();
                }
            });

            $('#dg').datagrid({
                title: '车辆服务列表',
                iconCls: 'icon-save',
                url: '../' + ctrlName + '/List',
                sortName: id,
                sortOrder: 'asc',
                remoteSort: false,
                idField: id,
                frozenColumns: [[
	                { field: 'ck', checkbox: true },
	                { title: '编号', field: id, width: 80, sortable: true },
                    { field: 'EmployeeNO', title: '司机编号', width: 80 },
                    { field: 'VehicleNO', title: '车辆编号', width: 80 }
				]],
                columns: [[
                        { field: 'ContactUnit', title: '联系人', width: 100 },
                        { field: 'ContactPhone', title: '联系电话', width: 100 },
                        { field: 'MeetAddress', title: '接车地点', width: 100 },
                        { field: 'PlanTime', title: '预约时间', width: 120,
                            formatter: function (value) {
                                return JsonToDateString(value, 'yyyy-MM-dd hh:mm:ss');
                            }
                        },
                        { field: 'DepartureTime', title: '出发时间', width: 120,
                            formatter: function (value) {
                                return JsonToDateString(value, 'yyyy-MM-dd hh:mm:ss');
                            }
                        },
                        { field: 'ReturnTime', title: '回馆时间', width: 120,
                            formatter: function (value) {
                                return JsonToDateString(value, 'yyyy-MM-dd hh:mm:ss');
                            }
                        }
                    ]],
                pagination: true,
                fitColumns: true,
                toolbar: toolbar
            });
        });      
    </script>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server"> 
    <div id="dlg-buttons2">
		<a href="#"  class="easyui-linkbutton" iconCls="icon-ok" onclick="crud.Save2()" >保存</a>
		<a href="#"  class="easyui-linkbutton" iconCls="icon-cancel" onclick="crud.Cancel2()" >取消</a>
	</div>
    <div id="dlgReturnVehicle" class="easyui-dialog" closed="true" buttons="#dlg-buttons2" 
			style="width:600px;height:420px;padding:10px 20px">
		<div class="ftitle">回馆登记</div>
		<form id="fmReturnVehicle" method="post"> 
            <table>
                <tr>
                    <td align="right" width="80" >编号:</td>
                    <td>
                        <input name="UseVehicleNO" readonly="readonly"  class="easyui-validatebox  readonly"/>
                        <input name="ServiceNo" type="hidden" />
                    </td>
                    <td align="right" >预约时间:</td>
                    <td><input name="PlanTime"  class="easyui-validatebox  readonly" readonly="readonly"/>                        
                    </td>
                </tr>
                <tr>
                    <td align="right" >联系人:</td>
                    <td><input name="ContactUnit"  class="easyui-validatebox  readonly" readonly="readonly" /></td>
                    <td align="right" >联系电话:</td>
                    <td><input name="ContactPhone"  class="easyui-validatebox  readonly" readonly="readonly" /></td>
                </tr>
                <tr>
                    <td align="right" >接车地点:</td>
                    <td><input name="MeetAddress"  class="easyui-validatebox  readonly" readonly="readonly" /></td>
                    <td align="right" >运送地点:</td>
                    <td><input name="SendAddress"  class="easyui-validatebox  readonly" readonly="readonly" /></td>
                </tr>                                 
                <tr>
                    <td align="right" >用途：</td>
                    <td>
                        <input name="Purpose"  class="easyui-validatebox  readonly" readonly="readonly" />                        
                    </td>
                    <td align="right" >要求：</td>
                    <td>
                        <input name="Requirements"  class="easyui-validatebox  readonly" readonly="readonly" />                        
                    </td>
                </tr>                 
                <tr>
                    <td align="right" >司机:</td>
                    <td> <input name="EmployeeNO"  class="easyui-validatebox  readonly" readonly="readonly"  /></td>
                    <td align="right" >车辆:</td>
                    <td> <input name="VehicleNO"  class="easyui-validatebox  readonly" readonly="readonly"  /></td>
                </tr>  
                <tr>
                    <td align="right" >备注:</td>
                    <td colspan="3"><textarea name="Remark" class="readonly" style="width:98%" readonly="readonly" ></textarea></td>
                </tr>
                <tr>
                    <td align="right" >死者编号:</td>
                    <td> <input name="DeadNO" id="DeadNO" type="text"  /></td>
                    <td align="right" >公里数:</td>
                    <td> <input name="MileAge" class="easyui-numberbox" precision="0" required="true" /></td>
                </tr>   
                <tr>
                    <td align="right" >运费:</td>
                    <td colspan="3"> <input name="Charges" class="easyui-numberbox" precision="2" required="true"/></td>                    
                </tr>       
            </table>
            <div><%Html.RenderPartial("UCSelectDeadNo"); %></div>           			            
		</form>        
	</div>   
    <div id="dlg" class="easyui-dialog" closed="true" buttons="#dlg-buttons" 
			style="width:600px;height:380px;padding:10px 20px">
		<div class="ftitle">车辆服务信息</div>
		<form id="fm" method="post"> 
            <table>
                <tr>
                    <td align="right" width="80" >编号:</td>
                    <td>
                        <input name="UseVehicleNO" readonly="readonly" style="background-color:Silver" class="easyui-validatebox"/>
                        <input id="ServiceNo" name="ServiceNo" type="hidden" />
                    </td>
                    <td align="right" >预约时间:</td>
                    <td><input name="PlanTime"  class="easyui-datetimebox" style="width:150" />
                        <input name="DeadNO" type="hidden"/>
                    </td>
                </tr>
                <tr>
                    <td align="right" >联系人:</td>
                    <td><input name="ContactUnit"  class="easyui-validatebox" required="true" /></td>
                    <td align="right" >联系电话:</td>
                    <td><input name="ContactPhone"  class="easyui-validatebox" required="true" /></td>
                </tr>
                <tr>
                    <td align="right" >接车地点:</td>
                    <td><input name="MeetAddress"  class="easyui-validatebox" required="true" /></td>
                    <td align="right" >运送地点:</td>
                    <td><input name="SendAddress"  class="easyui-validatebox" required="true" /></td>
                </tr>                                 
                <tr>
                    <td align="right" >用途：</td>
                    <td>
                        <input name="Purpose"  class="easyui-validatebox" />                        
                    </td>
                    <td align="right" >要求：</td>
                    <td>
                        <input name="Requirements"  class="easyui-validatebox" />                        
                    </td>
                </tr>                 
                <tr>
                    <td align="right" >选择司机:</td>
                    <td><input id="EmployeeNO" name="EmployeeNO"  class="easyui-validatebox" readonly="readonly"  /></td>
                    <td align="right" >选择车辆:</td>
                    <td><input id="VehicleNO" name="VehicleNO"  class="easyui-validatebox" readonly="readonly"  /></td>
                </tr>  
                <tr>
                    <td align="right" >备注:</td>
                    <td colspan="3"><textarea name="Remark" style="width:98%"></textarea></td>
                </tr>            
            </table>           			            
		</form>
        <div id="dlgSelect">
            <div id="selectList"></div>
        </div>
	</div>          
</asp:Content>


